﻿Public Class clsHWSetting

    Public Function selectForLHJByID(ws As String, id As String) As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * "
        szSql &= "  FROM sf.t_hw_setting "
        szSql &= "  WHERE factory_id =? "
        szSql &= "  AND workshop_id = ? "
        szSql &= "  AND equip_type_id = ?"
        szSql &= "  AND equip_id =? "
        szSql &= "  ORDER BY equip_id "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, ws, g_equiptype_LHJ, id)
        If IsNothing(dt) Then
            selectForLHJByID = New DataTable
        Else
            selectForLHJByID = dt
        End If
    End Function

    Public Function selectAllLHJ() As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * "
        szSql &= "  FROM sf.t_hw_setting "
        szSql &= "  WHERE factory_id =? "
        szSql &= "  AND workshop_id = ? "
        szSql &= "  AND equip_type_id = ?"
        szSql &= "  AND deleted = false"
        'szSql &= "  AND equip_id = '800T-21'"

        szSql &= "  ORDER BY equip_id ASC"

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, g_ws_lh, g_equiptype_LHJ)
        If IsNothing(dt) Then
            selectAllLHJ = New DataTable
        Else
            selectAllLHJ = dt
        End If
    End Function

    Public Function getSpecInterval(_prod_id As String) As Integer
        Dim szSql As String = ""
        szSql = "select lhj_interval_time from sf.t_prod_spec where  factory_id=? and prod_spec_id=("
        szSql &= "Select prod_spec_id from sf.b_prod_info where factory_id=? and prod_id=?)"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, _prod_id)
        If IsNothing(dt) = False Then
            getSpecInterval = CInt(dt.Rows(0)(0)) * 60 * 1000
        Else
            getSpecInterval = 0
        End If
    End Function
    Public Function getSpecIntervalbySpecID(_specid As String) As Integer
        Dim szSql As String = ""
        If _specid = "" Then Return 0
        szSql = "select lhj_interval_time from sf.t_prod_spec where  factory_id=? and prod_spec_id=?"

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, _specid)
        If IsNothing(dt) = False Then
            If dt.rows.Count > 0 Then
                getSpecIntervalbySpecID = CInt(dt.Rows(0)(0)) * 60 * 1000
            Else
                getSpecIntervalbySpecID = 0
            End If
        Else
            getSpecIntervalbySpecID = 0
        End If
    End Function
    Public Sub getMaxRowCol(ByRef _maxRow As Integer, ByRef _maxCol As Integer)
        Dim szSql As String = ""
        szSql &= "select max(loc_row) as maxrow,max(loc_col) as maxcol from sf.t_hw_setting "
        szSql &= " where workshop_id = 'LH' and equip_type_id = 'LHJ'"

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)

        _maxRow = dt.Rows(0)("maxrow")
        _maxCol = dt.Rows(0)("maxcol")

    End Sub
    ''' <summary>
    ''' 设置硫化机的状态
    ''' </summary>
    ''' <param name="_EquipID">硫化机ID</param>
    ''' <param name="_status">状态：0：闲、1：忙、2：失联、3：待出炉、4：温度or压力Error</param>
    ''' <returns></returns>
    Public Function setLHJStatus(_EquipID As String, _status As String) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " UPDATE sf.t_hw_setting "
        szSql &= "SET status = ? ,update_time=(select now())"
        szSql &= "WHERE   factory_id = ? "
        szSql &= "   and workshop_id = ?"
        szSql &= " and equip_type_id = ?"
        szSql &= " and equip_id = ?  "

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, _status, g_factory_id, g_ws_lh, g_equiptype_LHJ, _EquipID)
        Return rtn
    End Function

    Public Function setLHJSYSJ(_EquipID As String, _sysj As Integer) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " UPDATE sf.t_hw_setting "
        szSql &= "SET last_minutes = ? "
        szSql &= "WHERE   factory_id = ? "
        szSql &= "   and workshop_id = ?"
        szSql &= " and equip_type_id = ?"
        szSql &= " and equip_id = ?  "

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, _sysj, g_factory_id, g_ws_lh, g_equiptype_LHJ, _EquipID)
        Return rtn
    End Function

    Public Function setProdInfoTime(_prodID As String, _lh_total_duration As Integer, _lh_duration As Integer) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " UPDATE sf.b_prod_info "
        szSql &= "SET lh_total_duration = ? ,lh_duration=? ,update_time=(select now())"
        szSql &= "WHERE   factory_id = ? "
        szSql &= "   and prod_id = ?"

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, _lh_total_duration, _lh_duration, g_factory_id, _prodID)
        Return rtn
    End Function


    Public Function selectALL() As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * "
        szSql &= "  FROM sf.t_hw_setting "
        szSql &= "  WHERE factory_id =? "
        szSql &= "  AND workshop_id = ? "
        szSql &= "  AND equip_type_id = ?"
        szSql &= "  ORDER BY equip_id "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, "LH", "LHJ")
        If IsNothing(dt) Then
            selectALL = New DataTable
        Else
            selectALL = dt
        End If
    End Function
    ''' <summary>
    ''' 设置硫化机的状态
    ''' </summary>
    ''' <param name="_EquipID">硫化机ID</param>
    ''' <param name="_status">状态：0：闲、1：忙、2：失联、3：温度or压力Error、4：待出炉</param>
    ''' <returns></returns>
    Public Function setLHJOnOff(_EquipID As String, _status As String) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " UPDATE sf.t_hw_setting "
        szSql &= "SET status = ? ,update_time=(select now())"
        szSql &= "WHERE   factory_id = ? "
        szSql &= "   and workshop_id = ?"
        szSql &= " and equip_type_id = ?"
        szSql &= " and equip_id = ?  "

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, _status, g_factory_id, g_ws_lh, g_equiptype_LHJ, _EquipID)
        Return rtn
    End Function
    Public Function record_LHJ_Status(_lhj As String, _status As Integer) As Integer
        Dim Sql As String = String.Empty
        Try
            If _status = 0 Then

                Sql = "select * from sf.b_lhj_power where equip_id=? and off_dt is null"
                Dim dt As DataTable = DBacs.ExecuteSql(Sql, -1, _lhj)
                If dt.Rows.Count = 0 Then
                    Sql = "INSERT INTO sf.b_lhj_power(equip_id,on_dt) VALUES (?,?)"
                    Return DBacs.ExecuteUpdateSql(Sql, -1, _lhj, Now())
                ElseIf dt.Rows.Count > 0 Then
                    '------------每个月25日 23：59 固定加入完工时间-------------
                    If Now.Day = 25 And Now.Hour = 23 And (Now.Minute = 58 Or Now.Minute = 59) Then
                        Sql &= "update sf.b_lhj_power "
                        Sql &= "set off_dt=? "
                        Sql &= "where equip_id=? and off_dt is null "
                        Return DBacs.ExecuteUpdateSql(Sql, -1, Now, _lhj)
                    End If
                    '-----------------------------------------------------------

                End If
            ElseIf _status = 2 Then
                Sql &= "update sf.b_lhj_power "
                Sql &= "set off_dt=? "
                Sql &= "where equip_id=? and off_dt is null "
                Return DBacs.ExecuteUpdateSql(Sql, -1, Now, _lhj)
            End If
            Return 1
        Catch ex As Exception
            Return -2
        End Try

    End Function
End Class

